import {
  MenuItemDefinition,
  ObjectTreeMenuProps,
  Types,
  pickedModelData,
} from '@amcax/base';
import { Picker3D, PickerMgr } from '@amcax/renderer';
import { basicRefIDs } from '@app-cad-env/product/widgets/LeftPanel';
import { cmdMgr } from '@app-cad/manager/CmdManager';
import { ctx2 } from '@app-cad/module';

export const MenuItemFitSelectedObjects: MenuItemDefinition<ObjectTreeMenuProps> =
  {
    id: 'relation',
    children: '聚焦所选对象',
    hidden: ({ props: { documentObject } }) =>
      basicRefIDs.includes(documentObject.dxid) ||
      [
        Types.DocRoot,
        Types.GeomBox,
        Types.GeomSet,
        Types.FRAME,
        Types.PLANE,
        Types.PLANE_COPY,
        Types.LAW_CURVE,
        Types.PARAMETER_SET,
        Types.PARAMETER_OBJECT,
        Types.PARAMETER_LINK_ROOT,
        Types.PARAMETER_LINK,
        Types.DESIGN_TABLE,
        Types.DESIGN_TABLE_CONFIGURATION,
        Types.MeasureResultRoot,
        Types.MeasureObject,
      ].includes(documentObject.type),
    onClick: async ({ props: { documentObject } }) => {
      if (!documentObject.isRecursiveVisible) {
        const dxid = documentObject.dxid;
        await cmdMgr.fetchDependentShapeInfo([dxid]);

        const picker = PickerMgr.getPicker<Picker3D>('3d');
        const pickedObject = picker.generatePickObjByPickID(
          dxid,
        ) as pickedModelData;

        if (pickedObject?.assisObj) {
          ctx2.viewer.sceneSetup.fitTargetObject([pickedObject.assisObj]);
        }
      } else {
        ctx2.viewer.sceneSetup.fitTargetObject([ctx2.cadScene.assisGroup]);
      }
    },
  };
